package com.cjs.hegui30;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.content.SharedPreferences;
import android.content.Context;

public class ConfigProvider extends ContentProvider {
    public static final Uri CONTENT_URI = Uri.parse("content://com.cjs.hegui30.config/settings");
    private static final String DEFAULT_SERVER_ADDRESS = "127.0.0.1";
    private static final String DEFAULT_SERVER_PORT = "8080";
    private static final String PREF_NAME = "hegui30_config";
    private static final String PREF_SERVER_ADDRESS = "server_address";
    private static final String PREF_SERVER_PORT = "server_port";

    private SharedPreferences prefs;

    @Override
    public boolean onCreate() {
        prefs = getContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        // 确保有默认值
        if (!prefs.contains(PREF_SERVER_ADDRESS)) {
            prefs.edit().putString(PREF_SERVER_ADDRESS, DEFAULT_SERVER_ADDRESS).apply();
        }
        if (!prefs.contains(PREF_SERVER_PORT)) {
            prefs.edit().putString(PREF_SERVER_PORT, DEFAULT_SERVER_PORT).apply();
        }
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        MatrixCursor cursor = new MatrixCursor(new String[] { "key", "value" });

        String address = prefs.getString(PREF_SERVER_ADDRESS, DEFAULT_SERVER_ADDRESS);
        String port = prefs.getString(PREF_SERVER_PORT, DEFAULT_SERVER_PORT);

        cursor.addRow(new Object[] { PREF_SERVER_ADDRESS, address });
        cursor.addRow(new Object[] { PREF_SERVER_PORT, port });

        return cursor;
    }

    @Override
    public String getType(Uri uri) {
        return "vnd.android.cursor.dir/vnd.hegui30.config";
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        if (values != null) {
            String key = values.getAsString("key");
            String value = values.getAsString("value");
            if (key != null && value != null) {
                prefs.edit().putString(key, value).apply();
            }
        }
        return uri;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        if (values != null) {
            String key = values.getAsString("key");
            String value = values.getAsString("value");
            if (key != null && value != null) {
                prefs.edit().putString(key, value).apply();
                return 1;
            }
        }
        return 0;
    }

    // 公共方法用于保存配置
    public static void saveConfig(Context context, String address, String port) {
        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(PREF_SERVER_ADDRESS, address);
        editor.putString(PREF_SERVER_PORT, port);
        editor.apply();
    }

    // 公共方法用于获取配置
    public static String[] getConfig(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        String address = prefs.getString(PREF_SERVER_ADDRESS, DEFAULT_SERVER_ADDRESS);
        String port = prefs.getString(PREF_SERVER_PORT, DEFAULT_SERVER_PORT);
        return new String[] { address, port };
    }
}
